package com.web.oa.service.impl;

import com.web.oa.mapper.EmployeeMapper;
import com.web.oa.mapper.SysPermissionMapperCustom;
import com.web.oa.mapper.SysUserRoleMapper;
import com.web.oa.pojo.*;
import com.web.oa.service.EmployeeService;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("employeeService")
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private SysPermissionMapperCustom sysPermissionMapperCustom;
    @Autowired
    private SysUserRoleMapper userRoleMapper;

    /**登录
     * @return*/
    @Override
    public Employee login(String username, String password) {
        EmployeeExample ee=new EmployeeExample();
        EmployeeExample.Criteria criteria=ee.createCriteria();
        criteria.andEmailEqualTo(username);
        criteria.andEmailEqualTo(password);

        List<Employee> employees = employeeMapper.selectByExample(ee);

        return employees.size()>0?employees.get(0):null;
    }

    @Override
    public Employee findSysUserByUserCode(String username) {
        EmployeeExample employeeExample=new EmployeeExample();
        EmployeeExample.Criteria criteria=employeeExample.createCriteria();
        criteria.andNameEqualTo(username);

        List<Employee> list = employeeMapper.selectByExample(employeeExample);
        if(list!=null && list.size()>0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public Employee findEmployeeByManager(Long managerId) {
        return employeeMapper.selectByPrimaryKey(managerId);
    }
    //添加用户
    @Override
    public int saveUser(Employee employee) {
        return employeeMapper.insert(employee);
    }
    //查询用户和角色列表
    @Override
    public List<EmployeeCustom> findUserAndRoleList() {
        return sysPermissionMapperCustom.findUserAndRoleList();
    }

    @Override
    public void updateEmployeeRole(String roleId, String userId) {
        SysUserRoleExample example = new SysUserRoleExample();
        SysUserRoleExample.Criteria criteria = example.createCriteria();
        criteria.andSysUserIdEqualTo(userId);

        SysUserRole userRole = userRoleMapper.selectByExample(example).get(0);
        userRole.setSysRoleId(roleId);

        userRoleMapper.updateByPrimaryKey(userRole);
    }

    @Override
    public List<Employee> findEmployeeByLevel(int level) {
        EmployeeExample example = new EmployeeExample();
        EmployeeExample.Criteria criteria = example.createCriteria();
        criteria.andRoleEqualTo(level);
        List<Employee> list = employeeMapper.selectByExample(example);

        return list;
    }
}
